package cn.eeepay.framework.dao.workOrder;


import cn.eeepay.framework.db.pagination.Page;
import cn.eeepay.framework.model.AgentInfo;
import cn.eeepay.framework.model.workOrder.WaringConfigInfo;
import cn.eeepay.framework.model.workOrder.WorkWarningInfo;
import cn.eeepay.framework.util.StringUtil;
import org.apache.commons.lang3.StringUtils;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.ResultType;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.SelectProvider;
import org.apache.ibatis.jdbc.SQL;

import java.util.List;
import java.util.Map;

public interface WorkWarningReadDao {


    /**
     * 统计当月预警数量
     *
     * @param filterInfo
     * @return
     */
    @Select(
            "select count(*) from work_warning_record " +
                    " where agent_no=#{info.agentNo} and work_product_type=#{info.workProductType} " +
                    " and create_time>=#{info.createTimeBegin} and create_time<=#{info.createTimeEnd} "
    )
    int countWaringMonth(@Param("info") WorkWarningInfo filterInfo);

    /**
     * 查询当月最新一条预警
     *
     * @param filterInfo
     * @return
     */
    @Select(
            "select * from work_warning_record " +
                    " where agent_no=#{info.agentNo} and work_product_type=#{info.workProductType} " +
                    " and create_time>=#{info.createTimeBegin} and create_time<=#{info.createTimeEnd} " +
                    " ORDER BY create_time desc  LIMIT 1"
    )
    WorkWarningInfo getWaringMonthNow(@Param("info") WorkWarningInfo filterInfo);

    /**
     * 获取预警实体
     *
     * @return
     */
    @Select(
            "select * from work_product_config where work_product_type='1' ORDER BY id desc LIMIT 1 "
    )
    WaringConfigInfo getWaringConfig();

    /**
     * 分页查询预警列表
     *
     * @param info
     * @param page
     * @return
     */
    @SelectProvider(type = SqlProvider.class, method = "selectWorkWarningList")
    @ResultType(WorkWarningInfo.class)
    List<WorkWarningInfo> selectWorkWarningList(@Param("info") WorkWarningInfo info,  @Param("page") Page<WorkWarningInfo> page);

    class SqlProvider {

        /**
         * 分页查询预警列表
         *
         * @return
         */
        public String selectWorkWarningList(Map<String, Object> param) {
            final WorkWarningInfo info = (WorkWarningInfo) param.get("info");
            return new SQL() {{
                SELECT("wwr.*,ai.agent_name,ai.agent_level");
                FROM("work_warning_record wwr");
                INNER_JOIN("agent_info ai on ai.agent_no=wwr.agent_no");
                if (info.getId() != null) {
                    WHERE("wwr.id=#{info.id}");
                }
                if (info.getCreateTimeBegin() != null) {
                    WHERE("wwr.create_time >= #{info.createTimeBegin}");
                }
                if (info.getCreateTimeEnd() != null) {
                    WHERE("wwr.create_time <= #{info.createTimeEnd}");
                }
                if(StringUtil.isNotBlank(info.getAgentNo())){
                    WHERE("wwr.agent_no = #{info.agentNo}");
                }
                ORDER_BY("wwr.create_time desc,wwr.id desc");
            }}.toString();
        }
    }
}
